/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.test.fjump2

import com.ages.entity.Actor
import com.ages.graphics.GLDraw
import com.ages.graphics.ViewportSwitcher
import com.ages.input.Keyboard
import com.ages.math.Vector3D
import com.sun.opengl.util.GLUT
import javax.media.opengl.GL

class FJPlayer(p:Vector3D,keys:Keyboard) extends Actor{
  pos = p
  var rotation = 0.0
  val SPEED = 0.03
  def think(){
    if(keys.space && vel.Y==0)
      vel = Vector3D(vel.X,0.5,0)

    if(keys.left)
      vel = vel plus Vector3D(-SPEED,0,0)

    if(keys.right)
      vel = vel plus Vector3D(SPEED,0,0)

    if(pos.Y< -3)
      {
        vel = Vector3D(vel.X,0,0)
        pos = Vector3D(pos.X,0,0)
      }
//      if(pos.Y>0)
        vel = vel minus Vector3D(0,0.015,0)
      vel = vel dot 0.97
      pos = pos plus vel

  }

  def draw(gl:GL)
  {

    def ball = () =>{
      val mat_ambient = Array[Float](0.7f,0.7f,0.7f,1)
      val mat_diffuse = Array[Float](0.1f,0.8f,0.1f,1)
      val mat_specular = Array[Float](1,1,1,1)
      val high_shininess = Array[Float](100.0f)
      val no_mat = Array[Float](0,0,0,1)
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, mat_ambient,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_diffuse,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, high_shininess,0);
      gl.glMaterialfv(GL.GL_FRONT, GL.GL_EMISSION, no_mat,0);
      gl.glColor4d(0,1,0,1)
      gl.glMatrixMode(GL.GL_MODELVIEW)
      val glut = new GLUT()
      glut.glutSolidSphere(0.80, 30, 30)
    }
    val spot:Vector3D = Vector3D(pos.X,pos.Y,pos.Z-7)
    ViewportSwitcher.setup3DView(gl)

    GLDraw draw ball at spot on gl
  }

  override def collidesWith(other:Actor):Boolean ={
    var res = false
    if(other.isInstanceOf[Platform])
      {
        val diff = pos minus other.getPos
        if(diff.Y<1 && diff.Y>0.3 && Math.abs(diff.X)<2.35 && vel.Y<0 )
          {
//            println("true for "+this)
            res = true
          }
      }
      res
  }

  override def handleCollision(other:Actor)
  {
            val opos = other.getPos
            pos = Vector3D(pos.X,opos.Y+1,opos.Z)
            vel = Vector3D(vel.X,0,vel.Z)
//            println("handling collision with "+other)
  }

}
